#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time : 2020/7/7 4:43
# @Author : LZQ
# @Version：V 0.1
# @File : models.py
# @desc :
from __init__ import db
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from werkzeug.security import generate_password_hash, check_password_hash
from decimal import *
import datetime, time


class User(db.Model):
    '''
    用户类
    '''
    __tablename__ = "sp_user"
    __table_args__ = {"extend_existing": True}
    user_id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(32))
    qq_open_id = db.Column(db.String(32))
    pwd_hash = db.Column(db.String(200))
    phone = db.Column(db.String(11))
    user_email = db.Column(db.String(50))
    user_email_code = db.Column(db.String(100))
    user_sex = db.Column(db.String(2))
    user_qq = db.Column(db.String(11))
    user_tel = db.Column(db.String(52))
    user_xueli = db.Column(db.String(20))
    user_hobby = db.Column(db.String(100))
    user_introduce = db.Column(db.String(100))
    is_active = db.Column(db.Integer, default=0)
    create_time = db.Column(db.Integer)
    update_time = db.Column(db.Integer)

    # 读
    @property
    def password(self):
        return ''

    # 写
    @password.setter
    def password(self, pwd):
        self.pwd_hash = generate_password_hash(pwd)

    # 对比
    def check_pwd(self, pwd):
        return check_password_hash(self.pwd_hash, pwd)

    def to_dict(self):
        return {'username': self.username, 'password': self.password}


class UserCart(db.Model):
    __tablename__ = "sp_user_cart"
    __table_args__ = {"extend_existing": True}
    cart_id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer)
    cart_info = db.Column(db.Text)
    updated_at = db.Column(db.Integer)
    delete_time = db.Column(db.Integer)

    def to_dict(self):
        return {
            'cart_id': self.cart_id,
            'user_id': self.user_id,
            'cart_info': self.cart_info,
            'updated_at': self.updated_at,
            'delete_time': self.delete_time
        }


class Attribute(db.Model):
    __tablename__ = "sp_attribute"
    __table_args__ = {"extend_existing": True}
    attr_id = db.Column(db.SmallInteger, primary_key=True)
    attr_name = db.Column(db.String(32))
    cat_id = db.Column(db.ForeignKey("sp_category.cat_id"))
    attr_sel = db.Column(db.Enum('only', 'many'))
    attr_write = db.Column(db.Enum('manual', 'list'))
    attr_vals = db.Column(db.Text)
    delete_time = db.Column(db.Integer)

    def to_dict(self):
        return {
            'attr_id': self.attr_id,
            'attr_name': self.attr_name,
            'cat_id': self.cat_id,
            'attr_sel': self.attr_sel,
            'attr_write': self.attr_write,
            'attr_vals': self.attr_vals,
            'delete_time': self.delete_time
        }

    def delete(self):
        Attribute.query.filter(Attribute.attr_id == self.attr_id).delete()


class Category(db.Model):
    __tablename__ = "sp_category"
    __table_args__ = {"extend_existing": True}
    cat_id = db.Column(db.SmallInteger, primary_key=True)
    cat_name = db.Column(db.String(255))
    cat_pid = db.Column(db.Integer)
    cat_level = db.Column(db.Integer)
    cat_deleted = db.Column(db.Integer, default=0)
    cat_icon = db.Column(db.String(255))
    cat_src = db.Column(db.Text)

    def to_dict(self):
        return {
            'cat_id': self.cat_id,
            'cat_name': self.cat_name,
            'cat_pid': self.cat_pid,
            'cat_level': self.cat_level,
            'cat_deleted': True if self.cat_deleted == 1 else False,
            'cat_icon': self.cat_icon,
            'cat_src': self.cat_src
        }

    def to_parent_dict(self):
        return {
            "cat_id": self.cat_id,
            'cat_name': self.cat_name
        }


class Consignee(db.Model):
    __tablename__ = "sp_consignee"
    __table_args__ = {"extend_existing": True}
    cgn_id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.ForeignKey("sp_user.user_id"))
    cgn_name = db.Column(db.String(32))
    cgn_address = db.Column(db.String(255))
    cgn_tel = db.Column(db.String(32))
    cgn_code = db.Column(db.String(32))
    delete_time = db.Column(db.String(32))

    def to_dict(self):
        return {
            'cgn_id': self.cgn_id,
            'user_id': self.user_id,
            'cgn_name': self.cgn_name,
            'cgn_address': self.cgn_address,
            'cgn_tel': self.cgn_tel,
            'cgn_code': self.cgn_code,
            'delete_time': self.delete_time
        }


class Express(db.Model):
    __tablename__ = "sp_express"
    __table_args__ = {"extend_existing": True}
    express_id = db.Column(db.Integer, primary_key=True)
    order_id = db.Column(db.Integer)
    express_com = db.Column(db.String(32))
    express_nu = db.Column(db.String(32))
    create_time = db.Column(db.Integer)
    update_time = db.Column(db.Integer)

    def to_dict(self):
        return {
            'express_id': self.express_id,
            'order_id': self.order_id,
            'express_com': self.express_com,
            'express_nu': self.express_nu,
            'create_time': self.create_time,
            'update_time': self.update_time
        }


class Goods(db.Model):
    __tablename__ = "sp_goods"
    __table_args__ = {"extend_existing": True}
    goods_id = db.Column(db.Integer, primary_key=True)
    goods_name = db.Column(db.String(255))
    goods_price = db.Column(db.DECIMAL)
    goods_number = db.Column(db.Integer)
    goods_weight = db.Column(db.SmallInteger)
    cat_id = db.Column(db.SmallInteger)
    goods_introduce = db.Column(db.Text)
    goods_big_logo = db.Column(db.String(100))
    goods_small_logo = db.Column(db.String(100))
    is_del = db.Column(db.Enum('0', '1', server_default='0'))
    add_time = db.Column(db.Integer)
    upd_time = db.Column(db.Integer)
    delete_time = db.Column(db.Integer)
    cat_one_id = db.Column(db.SmallInteger)
    cat_two_id = db.Column(db.SmallInteger)
    cat_three_id = db.Column(db.SmallInteger)
    hot_mumber = db.Column(db.Integer)
    is_promote = db.Column(db.SmallInteger)
    goods_state = db.Column(db.Integer)

    def to_dict(self):
        return {
            'goods_id': self.goods_id,
            'goods_name': self.goods_name,
            'goods_price': str(Decimal(self.goods_price).quantize(Decimal('0.00'))),
            'goods_number': self.goods_number,
            'goods_weight': self.goods_weight,
            'cat_id': self.cat_id,
            'goods_introduce': self.goods_introduce,
            'goods_big_logo': self.goods_big_logo,
            'goods_small_logo': self.goods_small_logo,
            'is_del': self.is_del,
            'add_time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.add_time)),
            'upd_time': self.upd_time,
            'delete_time': self.delete_time,
            'cat_one_id': self.cat_one_id,
            'cat_two_id': self.cat_two_id,
            'cat_three_id': self.cat_three_id,
            'hot_mumber': self.hot_mumber,
            'is_promote': self.is_promote,
            'goods_state': self.goods_state
        }


class GoodsAttr(db.Model):
    __tablename__ = "sp_goods_attr"
    __table_args__ = {"extend_existing": True}
    id = db.Column(db.Integer, primary_key=True)
    goods_id = db.Column(db.String(255))
    attr_id = db.Column(db.SmallInteger)
    attr_value = db.Column(db.String(32))
    add_price = db.Column(db.DECIMAL)

    def to_dict(self):
        return {
            'id': self.id,
            'goods_id': self.goods_id,
            'attr_id': self.attr_id,
            'attr_value': self.attr_value,
            'add_price': self.add_price
        }


class GoodsCats(db.Model):
    __tablename__ = "sp_goods_cats"
    __table_args__ = {"extend_existing": True}
    cat_id = db.Column(db.Integer, primary_key=True)
    parent_id = db.Column(db.Integer)
    cat_name = db.Column(db.String(50))
    is_show = db.Column(db.Integer)
    cat_sort = db.Column(db.Integer)
    data_flag = db.Column(db.Integer)
    create_time = db.Column(db.Integer)

    def to_dict(self):
        return {
            'cat_id': self.cat_id,
            'parent_id': self.parent_id,
            'cat_name': self.cat_name,
            'is_show': self.is_show,
            'cat_sort': self.cat_sort,
            'data_flag': self.data_flag,
            'create_time': self.create_time
        }


class GoodsPics(db.Model):
    __tablename__ = "sp_goods_pics"
    __table_args__ = {"extend_existing": True}
    pics_id = db.Column(db.Integer, primary_key=True)
    goods_id = db.Column(db.Integer)
    pics_big = db.Column(db.String(128))
    pics_mid = db.Column(db.String(128))
    pics_sma = db.Column(db.String(128))

    def to_dict(self):
        return {
            'pics_id': self.pics_id,
            'goods_id': self.goods_id,
            'pics_big': self.pics_big,
            'pics_mid': self.pics_mid,
            'pics_sma': self.pics_sma
        }


class Manager(db.Model):
    __tablename__ = "sp_manager"
    __table_args__ = {"extend_existing": True}
    mg_id = db.Column(db.Integer, primary_key=True)
    mg_name = db.Column(db.String(32))
    mg_pwd = db.Column(db.String(300))
    mg_time = db.Column(db.DateTime)
    role_id = db.Column(db.ForeignKey("sp_role.role_id"))
    mg_mobile = db.Column(db.String(11))
    mg_email = db.Column(db.String(32))
    mg_state = db.Column(db.Integer)
    mg_is_delete = db.Column(db.Integer)
    role = db.relationship("Role", backref="manager")

    def get_state(self):
        if self.mg_state == 0:
            return True
        else:
            return False;

    def generate_auth_token(self, expiration=600):
        s = Serializer(self.mg_pwd, expires_in=expiration)
        return {'id': self.mg_id}

    # 读
    @property
    def password(self):
        return ''

    # 写
    @password.setter
    def password(self, pwd):
        self.mg_pwd = generate_password_hash(pwd)

    # 对比
    def check_pwd(self, pwd):
        return check_password_hash(self.mg_pwd, pwd)

    def to_dict(self):
        return {
            'mg_id': self.mg_id,
            'mg_name': self.mg_name,
            'mg_pwd': self.mg_pwd,
            'mg_time': self.mg_time,
            'role_id': self.role_id,
            'mg_mobile': self.mg_mobile,
            'mg_email': self.mg_email,
            'mg_state': self.get_state(),
            "role": self.role.role_name if self.role else '暂未分配角色'
        }


class Order(db.Model):
    __tablename__ = "sp_order"
    __table_args__ = {"extend_existing": True}
    order_id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.ForeignKey("sp_user.user_id"))
    order_number = db.Column(db.String(32))
    order_price = db.Column(db.DECIMAL)
    order_pay = db.Column(db.Enum(
        "0",  # 未支付
        "1",  # 支付宝
        "2",  # 微信支付
        "3",  # 银行卡支付
    ), default="1", index=True)
    is_send = db.Column(db.Enum(
        "是",  # 是
        "否",  # 否
    ), default="false", index=True)
    trade_no = db.Column(db.String(32))
    order_fapiao_title = db.Column(db.Enum('个人', '公司'), server_default='个人', nullable=False)
    order_fapiao_company = db.Column(db.String(32))
    order_fapiao_content = db.Column(db.String(255))
    consignee_addr = db.Column(db.String(255))
    pay_status = db.Column(db.Enum(
        "0",  # 待支付
        "1"  # 已支付
    ), server_default="0", index=True, nullable=False)
    create_time = db.Column(db.Integer)
    update_time = db.Column(db.Integer)

    def to_dict(self):
        return {
            'order_id': self.order_id,
            'user_id': self.user_id,
            'order_number': self.order_number,
            'order_price': str(Decimal(self.order_price).quantize(Decimal('0.00'))),
            'order_pay': self.order_pay,
            'is_send': True if self.is_send == '是' else False,
            'trade_no': self.trade_no,
            'order_fapiao_title': self.order_fapiao_title,
            'order_fapiao_company': self.order_fapiao_company,
            'order_fapiao_content': self.order_fapiao_content,
            'consignee_addr': self.consignee_addr,
            'pay_status': self.pay_status,
            'create_time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.create_time)),
            'update_time': self.update_time
        }


class OrderGoods(db.Model):
    __tablename__ = "sp_order_goods"
    __table_args__ = {"extend_existing": True}
    id = db.Column(db.Integer, primary_key=True)
    order_id = db.Column(db.ForeignKey("sp_order.order_id"))
    goods_id = db.Column(db.ForeignKey("sp_goods.goods_id"))
    goods_price = db.Column(db.DECIMAL)
    goods_number = db.Column(db.DECIMAL)
    goods_total_price = db.Column(db.DECIMAL)

    def to_dict(self):
        return {
            'id': self.id,
            'order_id': self.order_id,
            'goods_id': self.goods_id,
            'goods_price': self.goods_price,
            'goods_number': self.goods_number,
            'goods_total_price': self.goods_total_price
        }


class Permission(db.Model):
    __tablename__ = "sp_permission"
    __table_args__ = {"extend_existing": True}
    ps_id = db.Column(db.SmallInteger, primary_key=True)
    ps_name = db.Column(db.String(32))
    ps_pid = db.Column(db.SmallInteger)
    ps_c = db.Column(db.String(32))
    ps_a = db.Column(db.String(32))
    ps_level = db.Column(db.Enum('0', '1', '2'), server_default='0', nullable=False)
    path = db.relationship("Permissionapi", backref="permission")

    def to_dict(self):
        return {
            'id': self.ps_id,
            'authName': self.ps_name,
            'desc': self.ps_c,
        }


class Permissionapi(db.Model):
    __tablename__ = "sp_permission_api"
    __table_args__ = {"extend_existing": True}
    id = db.Column(db.Integer, primary_key=True)
    ps_id = db.Column(db.ForeignKey("sp_permission.ps_id"))
    ps_api_service = db.Column(db.String(32))
    ps_api_action = db.Column(db.String(32))
    ps_api_path = db.Column(db.String(32))
    ps_api_order = db.Column(db.Integer)

    def to_dict(self):
        return {
            'id': self.id,
            'ps_id': self.ps_id,
            'ps_api_service': self.ps_api_service,
            'ps_api_action': self.ps_api_action,
            'ps_api_path': self.ps_api_path,
            'ps_api_order': self.ps_api_order
        }


class Report_1(db.Model):
    __tablename__ = "sp_report_1"
    __table_args__ = {"extend_existing": True}
    id = db.Column(db.Integer, primary_key=True)
    rp1_user_count = db.Column(db.Integer)
    rp1_area = db.Column(db.String(32))
    rp1_date = db.Column(db.Date)

    def to_dict(self):
        return {
            'id': self.id,
            'rp1_user_count': self.rp1_user_count,
            'rp1_area': self.rp1_area,
            'rp1_date': self.rp1_date
        }

    def date_to_string(self):
        return self.rp1_date.strftime("%Y-%m-%d")


class Report_2(db.Model):
    __tablename__ = "sp_report_2"
    __table_args__ = {"extend_existing": True}
    id = db.Column(db.Integer, primary_key=True)
    rp2_page = db.Column(db.String(32))
    rp2_count = db.Column(db.Integer)
    rp2_date = db.Column(db.Date)

    def to_dict(self):
        return {
            'id': self.id,
            'rp2_page': self.rp2_page,
            'rp2_count': self.rp2_count,
            'rp2_date': self.rp2_date
        }


class Report_3(db.Model):
    __tablename__ = "sp_report_3"
    __table_args__ = {"extend_existing": True}
    id = db.Column(db.Integer, primary_key=True)
    rp3_src = db.Column(db.String(32))
    rp3_count = db.Column(db.Integer)
    rp3_date = db.Column(db.DateTime)

    def to_dict(self):
        return {
            'id': self.id,
            'rp3_src': self.rp3_src,
            'rp3_count': self.rp3_count,
            'rp3_date': self.rp3_date
        }


class Role(db.Model):
    __tablename__ = "sp_role"
    __table_args__ = {"extend_existing": True}
    role_id = db.Column(db.Integer, primary_key=True)
    role_name = db.Column(db.String(32))
    role_desc = db.Column(db.Text)
    ps_ids = db.Column(db.Text)

    def to_dict(self):
        return {
            'role_id': self.role_id,
            'role_name': self.role_name,
            'role_desc': self.role_desc,
            'ps_ids': self.ps_ids
        }


class Type(db.Model):
    __tablename__ = "sp_type"
    __table_args__ = {"extend_existing": True}
    type_id = db.Column(db.Integer, primary_key=True)
    type_name = db.Column(db.String(32))
    delete_time = db.Column(db.Integer)

    def to_dict(self):
        return {
            'type_id': self.type_id,
            'type_name': self.type_name,
            'delete_time': self.delete_time
        }
